package com.scoy.honeymei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.login.LoginActivity;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.PersonBean;
import com.scoy.honeymei.custem.CartEvent;
import com.scoy.honeymei.fragment.CartFragment;
import com.scoy.honeymei.fragment.HomeFragment;
import com.scoy.honeymei.fragment.MallMainFragment;
import com.scoy.honeymei.fragment.MeFragment;
import com.scoy.honeymei.fragment.OrderFragment;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    @BindView(R.id.frame_ly)
    FrameLayout frameLy;
    @BindView(R.id.btm0_tv)
    TextView btm0Tv;
    @BindView(R.id.btm1_tv)
    TextView btm1Tv;
    @BindView(R.id.btm2_tv)
    TextView btm2Tv;
    @BindView(R.id.btm3_tv)
    TextView btm3Tv;
    @BindView(R.id.line_view)
    View lineView;
    @BindView(R.id.btm4_tv)
    TextView btm4Tv;
    private MainActivity mContext;
    private int fragmentId = -1;
    private FragmentManager fm;
    private HomeFragment fg0;
    private MallMainFragment fg1;
    //  private ShopCartFragment fg2;
    private CartFragment fg2;
    private OrderFragment fg3;
    private MeFragment fg4;
    private long exitTime;

    private int isSrl = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mContext = MainActivity.this;

        fm = getSupportFragmentManager();
        setFragment(0);//默认界面

        initNormal();

        httpPerson();

    }


    @Override
    public void initNormal() {
        MyUtil.setStatusBar(mContext, getWindow(), true, 0);

        EventBus.getDefault().register(this);
    }


    @OnClick({R.id.btm0_tv, R.id.btm1_tv, R.id.btm2_tv, R.id.btm3_tv, R.id.btm4_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btm0_tv:
                setFragment(0);
                break;
            case R.id.btm1_tv:
                setFragment(1);
                break;
            case R.id.btm2_tv:
                setFragment(2);
                break;
            case R.id.btm3_tv:
                setFragment(3);
                break;
            case R.id.btm4_tv:
                setFragment(4);
                break;
        }
    }

    /**
     * 选中某一个
     *
     * @param current 当前选中item
     */
    private void setFragment(int current) {
        if (current == fragmentId) {
            return;
        }
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragments(transaction);
        switch (current) {
            case 0:
                fragmentId = 0;
                btm0Tv.setTextColor(getResources().getColor(R.color.colorMainBlue));
                btm0Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm0_checked, 0, 0);
                if (fg0 == null) {
                    fg0 = new HomeFragment();
                    transaction.add(R.id.frame_ly, fg0, "fg0");
                } else {
                    transaction.show(fg0);
                }

                fg0.setOnOneClick(new HomeFragment.OnOneClick() {
                    @Override
                    public void oneClick(int pos) {
                        setFragment(1);
                    }
                });

                break;
            case 1:
                fragmentId = 1;
                btm1Tv.setTextColor(getResources().getColor(R.color.colorMainBlue));
                btm1Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm1_checked, 0, 0);

                if (fg1 == null) {
                    fg1 = new MallMainFragment();
                    transaction.add(R.id.frame_ly, fg1, "fg1");
                } else {
                    transaction.show(fg1);
                }

                //商城跳购物车
             /*   fg1.setOnOneClick(new MallFragment.OnOneClick() {
                    @Override
                    public void oneClick(int pos) {
                        setFragment(2);
                    }
                });*/

                break;
            case 2:
                fragmentId = 2;
                btm2Tv.setTextColor(getResources().getColor(R.color.colorMainBlue));
                btm2Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm2_checked, 0, 0);
                if (fg2 == null) {
                    //  fg2 = new ShopCartFragment();
                    fg2 = CartFragment.newInstance();
                    transaction.add(R.id.frame_ly, fg2, "fg2");
                } else {
                    if (isSrl != 2) {
                        transaction.show(fg2);
                    } else {
                        fg2 = CartFragment.newInstance();
                        transaction.add(R.id.frame_ly, fg2, "fg2");
                    }
                }
                break;
            case 3:
                fragmentId = 3;
                btm3Tv.setTextColor(getResources().getColor(R.color.colorMainBlue));
                btm3Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm3_checked, 0, 0);
                if (fg3 == null) {
                    fg3 = new OrderFragment();
                    transaction.add(R.id.frame_ly, fg3, "fg3");
                } else {
                    transaction.show(fg3);
                }
                break;
            case 4:
                fragmentId = 4;
                btm4Tv.setTextColor(getResources().getColor(R.color.colorMainBlue));
                btm4Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm4_checked, 0, 0);
                if (fg4 == null) {
                    fg4 = new MeFragment();
                    transaction.add(R.id.frame_ly, fg4, "fg4");
                } else {
                    transaction.show(fg4);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 隐藏所有fragment
     *
     * @param transaction a
     */
    private void hideFragments(FragmentTransaction transaction) {
        initState();
        if (fg0 != null) {
            transaction.hide(fg0);
        }
        if (fg1 != null) {
            transaction.hide(fg1);
        }
        if (fg2 != null) {
            transaction.hide(fg2);
        }
        if (fg3 != null) {
            transaction.hide(fg3);
        }
        if (fg4 != null) {
            transaction.hide(fg4);
        }
    }

    /**
     * 恢复默认
     */
    private void initState() {
        btm0Tv.setTextColor(getResources().getColor(R.color.colorMainGray7));
        btm1Tv.setTextColor(getResources().getColor(R.color.colorMainGray7));
        btm2Tv.setTextColor(getResources().getColor(R.color.colorMainGray7));
        btm3Tv.setTextColor(getResources().getColor(R.color.colorMainGray7));
        btm4Tv.setTextColor(getResources().getColor(R.color.colorMainGray7));
        btm0Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm0_uncheck, 0, 0);
        btm1Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm1_uncheck, 0, 0);
        btm2Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm2_uncheck, 0, 0);
        btm3Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm3_uncheck, 0, 0);
        btm4Tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.btm4_uncheck, 0, 0);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("fragment_id", fragmentId);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        FragmentManager mFM = getSupportFragmentManager();
        fg0 = (HomeFragment) mFM.findFragmentByTag("fg0");
        fg1 = (MallMainFragment) mFM.findFragmentByTag("fg1");
        // fg2 = (ShopCartFragment) mFM.findFragmentByTag("fg2");
        fg2 = (CartFragment) mFM.findFragmentByTag("fg2");
        fg3 = (OrderFragment) mFM.findFragmentByTag("fg3");
        fg4 = (MeFragment) mFM.findFragmentByTag("fg4");
        setFragment(savedInstanceState.getInt("fragment_id"));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x871:
                //详情跳购物车
                setFragment(2);
                break;
            case 0x88:
                finish();
                startActivity(new Intent(mContext, LoginActivity.class));
                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            if(fragmentId!=0){
                setFragment(0);
            }else {
                MyUtil.mytoast(mContext, "再按一次退出程序");
                exitTime = System.currentTimeMillis();
            }
        } else {
            System.exit(0);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCartEvent(CartEvent event) {
        isSrl = 2;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //获取个人信息
    private void httpPerson() {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(mContext, MyUrl.PERSON_DATE, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                PersonBean personBean = new Gson().fromJson((String) o, PersonBean.class);

                int fileSawInt = personBean.getMovie_guide_status();
                //0=未观看,1=已观看
                SPHelper.applyInt(MeConstant.STATE_SHOW, fileSawInt);
            }
        });

    }


}
